package com.bobi.tfca.controller.message;

import com.bobi.tfca.model.entity.MemberEntity;
import com.bobi.tfca.model.pojo.R;
import com.bobi.tfca.model.vo.MessageVO;
import com.bobi.tfca.service.MessageService;
import com.bobi.tfca.web.annotation.CurrentUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 李文发
 * @date 2024/4/10
 */
@RestController
@RequestMapping("/notification")
@Api(tags = "通知接口")
@Slf4j
public class NotificationController {

    @Autowired
    private MessageService messageService;

    @GetMapping("/findAllNotifications")
    @ApiOperation(value = "查询全部通知")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "member", value = "用户信息")
    })
    public R<List<MessageVO>> findAllNotifications(@CurrentUser MemberEntity member) {
        Integer memberId = member.getMemberId();
        List<MessageVO> list = messageService.findAllNotifications(memberId);
        return R.data(list);
    }

    @GetMapping("/getNotificationById/{notificationId}")
    @ApiOperation(value = "查询通知详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "notificationId", value = "通知id")
    })
    public R<MessageVO> getNotificationById(@CurrentUser MemberEntity member, @PathVariable("notificationId") Long notificationId) {
        MessageVO messageVO = messageService.getNotificationById(member, notificationId);
        return R.data(messageVO);
    }

    @PostMapping("/publishNotification")
    @ApiOperation(value = "发布通知")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "member", value = "用户信息"),
            @ApiImplicitParam(name = "letterContent", value = "内容")
    })
    public R<String> publishNotification(@CurrentUser MemberEntity member, @RequestBody String notificationContent) {
        Integer memberId = member.getMemberId();
        Boolean success = messageService.publishNotification(memberId, notificationContent);
        return success ? R.success("已发送") : R.failed("发送失败");
    }

    @ApiOperation(value = "查询未读通知数量")
    @GetMapping("/getUnReadNotificationNum")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "member", value = "用户信息")})
    public R<Integer> getUnReadNotificationNum(@CurrentUser MemberEntity member) {
        Integer memberId = member.getMemberId();
        Integer unReadNotificationNum = messageService.getUnReadNotificationNum(memberId);
        return R.data(unReadNotificationNum);
    }
}
